package cdu.javaendwork.Controller.AdoptionMore;

import cdu.javaendwork.model.sqlModel.Post;
import cdu.javaendwork.model.sqlModel.Remark;
import cdu.javaendwork.model.sqlModel.Reply;
import cdu.javaendwork.model.sqlModel.User;
import cdu.javaendwork.service.Impl.AdoptionMore.PostServiceImpl;
import cdu.javaendwork.service.Impl.AdoptionMore.RemarkServiceImpl;
import cdu.javaendwork.service.Impl.AdoptionMore.ReplyServiceImpl;
import cdu.javaendwork.service.Impl.User.UserServiceImpl;
import cdu.javaendwork.service.Interface.AdoptionMore.PostService;
import cdu.javaendwork.service.Interface.AdoptionMore.RemarkService;
import cdu.javaendwork.service.Interface.AdoptionMore.ReplyService;
import cdu.javaendwork.service.Interface.User.UserService;
import cdu.javaendwork.util.pageHelp.PageHelper;
import cdu.javaendwork.util.pageHelp.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@WebServlet("/add_reply")
public class AddReply extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Remark remark=null;
        Post post=null;
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset = utf-8");

        ReplyService replyService = new ReplyServiceImpl();
        UserService userService = new UserServiceImpl();
        RemarkService remarkService=new RemarkServiceImpl();
        PostService postService = new PostServiceImpl();

        Set<Integer> set = new HashSet<>();
        List<User> userList=new ArrayList<>();

        String rid = req.getParameter("rid");
        int id = (rid == null || rid.equals("")) ? 1 : Integer.parseInt(rid);
        req.setAttribute("remarkid" , id);
        remark=remarkService.findById(id);

        String Page = req.getParameter("page");
        int pageIndex = (Page == null || Page.equals("")) ? 1 : Integer.parseInt(Page);
        int pageSize = 4;

        PageHelper pageHelper = new PageHelper();
        PageInfo pageInfo = pageHelper.filter("Reply","remarkid="+remark.getId(),pageIndex,pageSize);
        req.setAttribute("pageInfo",pageInfo);
        List<Reply> replyList = replyService.filterByRemarkIdLimit(remark.getId(), pageIndex, pageSize);
        for (Reply reply1:replyList){
            int userid = reply1.getUserid();
            if(set.contains(userid)) continue;
            User user= userService.findById(userid);
            userList.add(user);
            set.add(userid);
        }
        req.setAttribute("userList",userList);
        req.setAttribute("replyList",replyList);
        req.setAttribute("remark",remark);


        post = postService.findByid(remark.getPostid());

        req.setAttribute("post",post);
        req.getRequestDispatcher("addReply.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset = utf-8");
        User user = (User) req.getSession().getAttribute("user");
        String rid = req.getParameter("remarkid");
        int id = (rid == null || rid.equals("")) ? 1 : Integer.parseInt(rid);


        PrintWriter out=resp.getWriter();
        String content = req.getParameter("reply");
        ReplyService replyService =new ReplyServiceImpl();
        if(!replyService.insert(user.getId(),id,content)){
            out.println("<script>alert('回复失败！');window.location.href='add_reply?rid="+rid+"'</script>");
        }else{
            out.println("<script>alert('回复成功！');window.location.href='add_reply?rid="+rid+"'</script>");
        }

    }
}
